
import {takeLatest,put} from 'redux-saga/effects'
import * as types from '../../const/actionTypes'
import {LOGIN_SUCCESS} from "../actions/user";

/**
 * 异步登录
 * @param phone
 * @param password
 * @returns {IterableIterator<*>}
 */
export  function* login(action) {
    try {
        console.log(action.payload.phone+"login"+action.payload.password)
        //const result = yield call("api","param") TODO
        const testAvatar = {
            '1':"http://thirdwx.qlogo.cn/mmopen/vi_32/2DLyoLSH6XNqgiaSLSOUoVWIARcbKr08oXFOua3UH1fbxTl5mdqqHWImQcuDVcdkPBHtN8ib5s4o7ojAVmqSUl4Q/132",
            '2':"http://thirdwx.qlogo.cn/mmopen/vi_32/6ibBsxibjxCoptmevyHWRcLtqic5MjLzJKesF90EA8V5ibKicu8uLQqurnxQsdv8WmyA5KFZmn9FAceKqA1PKevrUTQ/132",
            '3':"http://thirdwx.qlogo.cn/mmopen/vi_32/icKVaJJ7o9BIz3qZkyBMztcWOBDtG9IUOz0S8QjKX9qEQvAh13M0UoNIMbaldCrkcoSiaWqIiass2Rpk6kNicw1Uew/132",
            '4':"http://thirdwx.qlogo.cn/mmopen/vi_32/mm2n7C4mHlOSGk3vc0gyWMyBQVA3qmvmibgcYx9dQ84iaIs8uTgK2J63OMMEaoxdYMAwwvVl6ic6Ewb1ORpF1Cgqg/132",
            '5':"http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJAgnPZctxThBCgJrMYevJfpJskvOrtemgXAicRH1F9zgSQbiaP3FTahhppXsYgeyq5mQrCJ7PDfdfw/132"
        }
        yield put(LOGIN_SUCCESS("asdasdasds",action.payload.password,action.payload.phone,testAvatar[action.payload.password])) //登录成功之后 调用socket
        yield put({type:types.REQUEST_SOCKET}) //启动socket
    }catch (e) {
        console.log('登录失败')
    }
}

/**
 * 监听登录
 * @returns {IterableIterator<*>}
 */
export function* watchLogin() {
        yield takeLatest(types.LOGIN_REQUEST,login)
}
